# Maintainer: Noel Kuntze <noel.kuntze@thermi.consulting>
pkgname=z-push
pkgver=2.6.2
pkgrel=0
pkgdesc="open-source implementation of the ActiveSync protocol"
arch="noarch"
url="https://z-push.org/"
license="GPL-3.0-only"
options="!check" # No test suite
depends="php7 php7-fpm php7-shmop php7-sysvsem php7-sysvshm"
pkgusers="z-push"
pkggroups="z-push"

install="z-push.pre-install"

source="$pkgname-$pkgver.tar.gz::https://github.com/Z-Hub/Z-Push/archive/refs/tags/$pkgver.tar.gz
	apache.example.conf
	htaccess
	z-push.ini
	z-push.conf
	nginx-location.conf
	nginx-ssl.example.conf
	php-fpm.example.conf
	z-push-admin
	z-push-top
	"

builddir="$srcdir/Z-Push-$pkgver"

prepare() {
	default_prepare
	sed "s/ZPUSHVERSION/$pkgver/" build/version.php.in > src/version.php
	sed -i -e "s|\('MAPI_SERVER', \).*$|\1'file:///var/run/kopano/server.sock'\);|" src/backend/kopano/config.php
	sed -i -e "s|\('BACKEND_PROVIDER', \).*$|\1'BackendKopano'\);|" src/config.php
}

package() {
	install -d "$pkgdir/etc/webapps/z-push" "$pkgdir/etc/php7/conf.d/" \
		"$pkgdir/etc/php7/php-fpm.d/" "$pkgdir/usr/share/webapps/z-push/" \
		"$pkgdir/usr/bin/"

	cp "$srcdir/z-push-admin" "$srcdir/z-push-top" "$pkgdir/usr/bin/"

	cp -r src/* "$pkgdir/usr/share/webapps/z-push/"

	install -Dm 0640 "$srcdir/htaccess" "$pkgdir/usr/share/webapps/z-push/.htaccess"

	install -Dm 0755 "$srcdir/z-push-admin" "$srcdir/z-push-top" "$pkgdir/usr/bin/"

	install -Dm 0644 "$srcdir/apache.example.conf" "$srcdir/php-fpm.example.conf" "$srcdir/nginx-ssl.example.conf" "$srcdir/nginx-location.conf" "$pkgdir/etc/webapps/z-push/"
	install -g z-push -m 0750 src/config.php "$pkgdir/etc/webapps/z-push/config.php"
	install -g z-push -m 0750 src/policies.ini "$pkgdir/etc/webapps/z-push/policies.ini"

	install -g z-push -dm 0750 "$pkgdir/etc/webapps/z-push/backends"

	cd src/backend
	for config_file in */config.php
	do
		install -Dm 0750 -g z-push "$config_file" "$pkgdir/etc/webapps/z-push/backends/$config_file"
		ln -sf "/etc/webapps/z-push/backends/$config_file" "$config_file"
	done

	ln -sf /etc/webapps/z-push/config.php "$pkgdir/usr/share/webapps/z-push/config.php"
	ln -sf /etc/webapps/z-push/policies.ini "$pkgdir/usr/share/webapps/z-push/policies.ini"

	install -Dm 0644 "$srcdir/z-push.ini" "$pkgdir/etc/php7/conf.d/"
	install -Dm 0644 "$srcdir/z-push.conf" "$pkgdir/etc/php7/php-fpm.d/"
	install -dm 0750 -o z-push -g z-push "$pkgdir/var/log/z-push/" "$pkgdir/var/lib/z-push"
}

sha512sums="deb7735e4eb296584d9ad9626d962068154794265233a5a0c5b057055bc92d31187667e00ce570545b17fb2502343305b75996625d6451bc2837901afecdde9a  z-push-2.6.2.tar.gz
cffa906fdf147e8e81ae5a6033762f5a0f5f33e718dca86b12aab973123d2ce094b8e3e7cffe4880403fb961626dc33cf54bb6b2d8a7a376976cdfc5ec4b9cf9  apache.example.conf
f6f71969576b5f4524d980cca55ae445691d3ce1a35973657770a631fb8e0a17375d67245802eaf1a5b819528602ba88165e6fa873db5462937087521aee394d  htaccess
ebdcb8905f6fd49bc073aa7e884252daae1c7b6865bfd5cf7ce3ecd889d78a57a4979b09a8b02fbf16cdd8dbb236de8493b1896ac2f0693ddf41bd8bf87df11d  z-push.ini
7d77fedb1f22ee77ba091d54051b946ad048c4f63d83a27585e95b68f46eebc2376032cf3c7d8112205736dd1fd4e2fb0c976cc7b510796b79109b146469208d  z-push.conf
e39d2c101708274e02923869083ed613c1ad1d8df574ca4f1f01f0a0ed7c044a7422bd8532e52d0db8f8279dc04506582747ac83cac978044db62fb7e213c200  nginx-location.conf
dad272b51824f21bfc2a5f41731cd20bca6e877b15756e1115304e9c353fbc4c98cbd1c66661e0974685d1a01e94b933d5c380c8a6b17aa18b65ab86cbfb4971  nginx-ssl.example.conf
bad70098b919c4cacce3152b276da489d7a36ca195b607508ad316211401dc98f07433ef505deb82a91388993e58b398cce2bc3eb295cf812362bae96b40fb35  php-fpm.example.conf
4f85dc2a316f2e5cce82c4a8b7310d5c8e8970c18d8726f9dec6bd5b5a6c657f4dd2f7f62dfead25be3c95dc26526c86f79ee1e7f6fd15248ad0429d0342b6dc  z-push-admin
1c21e770e2206b46e4950be7da3067fad6ee2bb2481156e50b169b93d155b89385003975aabeda3bd14fb4727bd26b518f7e370cb79ddad5a7786ef456d30e13  z-push-top"
